package com.quasar.hpatient.module.home_health;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.api.HttpClient;
import com.quasar.hpatient.api.HttpParams;
import com.quasar.hpatient.bean.home_health.HealthBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import lib.quasar.base.frame.BasePresenter;
import lib.quasar.base.frame.OnModelAcceptChangeListener;
import lib.quasar.context.BaseApp;
import lib.quasar.recycler.BaseCommonAdapter;
import lib.quasar.recycler.BaseCommonMultAdapter;
import lib.quasar.recycler.holder.RecyclerHolder;
import lib.quasar.recycler.manager.CrashLinearLayoutManager;
import lib.quasar.recycler.model.MultModel;
import lib.quasar.util.DimenUtil;
import lib.quasar.util.SpannableUtil;
import okhttp3.RequestBody;

/**
 * description: 首页 ==> 健康史
 * created by kalu on 2018/3/29 9:03
 */
public class HealthPresenter implements BasePresenter {

    private final ArrayList<HealthBean.HealthEventBean.HealthEventItemBean> mDatas = new ArrayList<>();

    void initList(HealthView view, RecyclerView recycler) {

        if (null == view || null == recycler) return;

        final BaseCommonMultAdapter<HealthBean.HealthEventBean.HealthEventItemBean> adapter = new BaseCommonMultAdapter<HealthBean.HealthEventBean.HealthEventItemBean>(mDatas) {

            @Override
            protected void onMult() {
                addMult(MultModel.TYPE_0, R.layout.activity_home_health_type0);
                addMult(MultModel.TYPE_1, R.layout.activity_home_health_type1);
            }

            @Override
            protected void onNext(RecyclerHolder holder, HealthBean.HealthEventBean.HealthEventItemBean model, int position) {

                switch (model.getMultType()) {
                    case MultModel.TYPE_0:
                        view.setText(holder, R.id.home_health_type0_age, model.getEvent_name());
                        view.setText(holder, R.id.home_health_type0_time, model.getEvent_datetime());
                        break;
                    case MultModel.TYPE_1:
                        view.setText(holder, R.id.home_health_type1_title, model.getTitie());
                        view.setImage(holder, R.id.home_health_image, R.drawable.ic_comm_patient_woman);
                        SpannableUtil.append(model.getDescription());
                        SpannableUtil.newline();
                        SpannableUtil.append(model.getDescription());
                        view.setText(holder, R.id.home_health_type1_message, SpannableUtil.build());

                        holder.setOnLongClickListener(v -> {
                            view.showSingle(position);
                            return true;
                        }, R.id.home_health_type1_title, R.id.home_health_type1_message);
                        break;
                }
            }
        };

        final View head = LayoutInflater.from(BaseApp.getContext()).inflate(R.layout.activity_home_health_head, null);
        final RecyclerView.LayoutParams params1 = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DimenUtil.dp2px(100));
        params1.bottomMargin = DimenUtil.dp2px(20);
        head.setLayoutParams(params1);
        adapter.addHead(head);
        head.findViewById(R.id.activity_home_health_head_time).setOnClickListener(v -> view.showTime());
        head.findViewById(R.id.activity_home_health_head_type).setOnClickListener(v -> view.showType());

        adapter.setNullView(BaseApp.getContext(), R.layout.layout_recycler_loading);

        final CrashLinearLayoutManager manager = new CrashLinearLayoutManager(BaseApp.getContext());
        view.setRecycler(recycler, adapter, manager);
    }

    void setType(HealthView view, RecyclerView recycler, String type) {

        if (null == view || null == recycler || TextUtils.isEmpty(type)) return;

        final RecyclerView.Adapter adapter = recycler.getAdapter();
        if (null == adapter || !(adapter instanceof BaseCommonAdapter)) return;

        BaseCommonAdapter adapter1 = (BaseCommonAdapter) adapter;
        final LinearLayout head = adapter1.getHead();
        if (null == head) return;

        TextView text = head.findViewById(R.id.activity_home_health_head_type);
        if (null == text) return;
        text.setText(type);
    }

    void setTime(HealthView view, RecyclerView recycler, String time) {

        if (null == view || null == recycler || TextUtils.isEmpty(time)) return;

        final RecyclerView.Adapter adapter = recycler.getAdapter();
        if (null == adapter || !(adapter instanceof BaseCommonAdapter)) return;

        BaseCommonAdapter adapter1 = (BaseCommonAdapter) adapter;
        final LinearLayout head = adapter1.getHead();
        if (null == head) return;

        TextView text = head.findViewById(R.id.activity_home_health_head_time);
        if (null == text) return;
        text.setText(time);
    }

    /**
     * all：全部
     * edit_record ：调药记录
     * event: 患者事件
     *
     * @param view
     * @param recycler
     * @param beginDate
     * @param endDate
     * @param type
     */
    void healthList(HealthView view, RecyclerView recycler, String beginDate, String endDate, String type) {

        final HashMap<String, Object> map = new HashMap<>();
        if (TextUtils.isEmpty(endDate)) {
            endDate = BaseApp.getResource().getString(R.string.http_date, String.valueOf(2000), String.valueOf(01), String.valueOf(01));
        }
        map.put(BEGIN_DATE, TextUtils.isEmpty(beginDate) ? getSecond() : beginDate);
        map.put(END_DATE, endDate);
        map.put(NUMBER, "20");
        map.put(TYPE, type);

        final RequestBody params2 = createParams(map, HttpParams.DATA_TYPE_3_5);
        final Observable observable = HttpClient.getSingleHolder().getHttpService().listHealth(params2);
        request(observable, new OnModelAcceptChangeListener<Map<String, HealthBean>>() {

            @Override
            public void modelSucc(Map<String, HealthBean> result) {

                if (null == result || result.size() == 0) {
                    return;
                }

                for (Map.Entry<String, HealthBean> entry : result.entrySet()) {

                    if (null == entry) continue;

                    final String key = entry.getKey();
                    if (TextUtils.isEmpty(key)) continue;

                    final HealthBean value = entry.getValue();
                    if (null == value) continue;

                    final HealthBean.HealthEventBean event = value.getEvent();
                    if (null == event) continue;

                    final List<HealthBean.HealthEventBean.HealthEventItemBean> list = event.getJson_event();
                    if (null == list || list.size() == 0) continue;

                    final HealthBean.HealthEventBean.HealthEventItemBean model = new HealthBean.HealthEventBean.HealthEventItemBean();
                    final String year = key.substring(0, 4);
                    final String monnth = key.substring(5, 10);
                    final String age = BaseApp.getStrings(R.string.home_health_age, year, value.getAge());
                    model.setEvent_name(age);
                    model.setEvent_datetime(monnth);
                    mDatas.add(model);
                    mDatas.addAll(list);
                }

                view.refreshList(recycler);
            }
        });
    }

    void addMolde(HealthView view, RecyclerView recycler, String time, String input) {
        view.toast(time + "<>" + input);
    }

    @Override
    public void recycler() {
    }
}
